package com.dindang.ddm.mvp.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dindang.ddm.R;
import com.dindang.ddm.mvp.model.entity.Build;
import com.dindang.ddm.mvp.ui.listener.DialogListener;
import com.longxi.gamer_base.model.User;
import com.longxi.gamer_base.widget.EmptyLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by cxw on & 2018/11/8
 */
public class RepairsAddressDialog extends Dialog {

    public RepairsAddressDialog(@NonNull Context context) {
        super(context);
    }

    public RepairsAddressDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected RepairsAddressDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder{
        private Context context;
        private DialogListener listener;
        private List<Build> list;
        private String title;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(String title){
            if("".equals(title)){
                this.title = "请设置标题";
            }else{
                this.title = title;
            }
            return this;
        }

        public Builder setProperty(DialogListener listener) {
            this.listener = listener;
            return this;
        }

        private TextView tv_title;
        private RecyclerView recyclerView;
        private TextView tv_cancle;
        private EmptyLayout emptylayout;
        private RepairsTypeViewAdapter adapter;

        private RepairsAddressDialog dialog;

        public RepairsAddressDialog create() {
            dialog = new RepairsAddressDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_list_bottom, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            tv_title = view.findViewById(R.id.tv_title);
            recyclerView = view.findViewById(R.id.recyclerView);
            tv_cancle = view.findViewById(R.id.tv_cancle);
            emptylayout = view.findViewById(R.id.emptylayout);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            tv_title.setText(title);
            tv_cancle.setOnClickListener(v -> dialog.dismiss());
            return dialog;
        }

        /**
         * 根据古老的登录接口获取员工所属小区
         * @param user
         */
        public void xiaJiBaXie(User user){
            List<String> buildidList= Arrays.asList(user.buildid.replace(" ", "").split(","));
            List<String> buildList= Arrays.asList(user.build.replace(" ", "").split(","));
            List<Build> localBuilds = new ArrayList<>();
            for (int i = 0; i < buildidList.size(); i++) {
                Build localBuild = new Build();
                localBuild.buildid = buildidList.get(i);
                localBuild.build =buildList.get(i);
                localBuilds.add(localBuild);
            }
            updateData(localBuilds);
        }

        public void updateData(List<Build> list){
            this.list = list;
            adapter = new RepairsTypeViewAdapter();
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            emptylayout.hide();
        }

        public void dismiss(){
            if(dialog != null){
                dialog.dismiss();
            }
        }

        class RepairsTypeViewAdapter extends RecyclerView.Adapter<RepairsTypeViewAdapter.ViewHolder> {

            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dialog_custom_list, parent, false);
                return new ViewHolder(v);
            }

            @Override
            public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
                Build build = list.get(position);
                holder.tv_name.setText(build.build);
                holder.tv_name.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.onClick(build.buildid, build.build);
                    }
                });
            }

            @Override
            public int getItemCount() {
                return list.size();
            }

            protected class ViewHolder extends RecyclerView.ViewHolder {
                TextView tv_name;
                public ViewHolder(View itemView) {
                    super(itemView);
                    tv_name = itemView.findViewById(R.id.tv_name);
                }
            }
        }
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setPadding(25, 0, 25, 25);
        getWindow().setAttributes(layoutParams);
    }

}
